import java.text.SimpleDateFormat
import java.util.regex.Pattern
import java.util.{Date, Locale}

import com.itstaredu.IpParser
import org.apache.spark.rdd.RDD

/**
  * Created by 王宏 on 2017/10/4.
  * MapUtil 用于字符分割处理数据的主程序
  */
class MapUtil extends java.io.Serializable {
  // 识别apache日志+cookie的正则表达式
  val APACHE_LOG_REGEX = "^([\\d.]+|-) (\\S+|-) (\\S+|-) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}|-) (\\d+|-) \"([^\"]+)\" \"([^\"]+)\""
  // 识别不带日志的+cookie的正则表达式
  val CANNOT_GET = "nothing"

  // 日志属性
  var ipAddress = CANNOT_GET
  var dateTime = CANNOT_GET
  var requests = CANNOT_GET
  var responseStats = CANNOT_GET
  var bytesSent = CANNOT_GET
  var referer = CANNOT_GET
  var browser = CANNOT_GET
  var areaAddress = CANNOT_GET
  var loaclAddress = CANNOT_GET
  var receiveTime = CANNOT_GET
  /*
  * map函数，用于处理line字符串流程
  * */
  def map(lines: RDD[String]) = lines.map(f = sline => {
    val line = sline.replace("bbs.moonseo.cn ", "")
    var pattern = Pattern.compile(APACHE_LOG_REGEX)
    val matcher = pattern.matcher(line)
    // 定义ip receiveTime url referUrl userAgent变量
    var ipStr: String = null
    // 正则匹配
    if (matcher.matches()) {
      ipAddress = matcher.group(1)
      dateTime = matcher.group(4)
      requests = matcher.group(5)
      responseStats = matcher.group(6)
      bytesSent = matcher.group(7)
      ipStr = ipAddress
      if(!matcher.group(8).equals("-")){
        referer = matcher.group(8)
      }
      browser = matcher.group(9)
      val ipParser = new IpParser

      try {
        areaAddress = ipParser.parse(ipStr).split(" ")(0)
        loaclAddress = ipParser.parse(ipStr).split(" ")(1)
      } catch {
        case e: Exception => e.printStackTrace()
      }
      val df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.US)

      try {
        val date: Date = df.parse(dateTime)
        // 获得指定时间
        receiveTime = date.getTime.toString
      } catch {
        case e: Exception => e.printStackTrace()
      }
      ipAddress + "\t" + dateTime +
        "\t" + requests + "\t" + responseStats + "\t" + bytesSent +
        "\t" + referer + "\t" + browser +
        "\t" + areaAddress + "\t" + loaclAddress +
        "\t" + receiveTime
    }
  })
}
